from flask import Blueprint,request,make_response,redirect,jsonify
from application.models.users.user import *
from utils.captcha.captcha import captcha
from utils.myredis import r
import redis
from utils.myjwt import myjwt
from qiniu import Auth

cc_blue = Blueprint("ccbule",__name__)


@cc_blue.route("/gettags")
def gettags():
    tags=Tags.query.all()
    tagslist=[{"id":i.id,"name":i.name} for i in tags]
    types=Ctype.query.all()
    typelist=[{"id":i.id,"name":i.name} for i in types]
    ctype=CourseType.query.all()
    clist=[{"id":i.id,"name":i.name} for i in ctype]
    return jsonify({"code":200,"tags":tagslist,"types":typelist,"clist":clist})


@cc_blue.route("/qniu")
def qniu():
    access_key = 'wTNsEnBS4nj89eJjYUSaMt-YK-459dnvuIMBv4k5'
    secret_key = '2xt_JPXSj37yqle1fMLaEAEpn-K_E8Gyuj4Tcr8H'
    q = Auth(access_key, secret_key)
    #要上传的空间
    bucket_name = 'h2203ab'
    #生成上传 Token，可以指定过期时间等
    token = q.upload_token(bucket_name)
    return  jsonify({'code':200,'token':token})



import json

@cc_blue.route("/addcouse",methods=['POST'])
def addcourse():
    try:
        data=json.loads(request.data)
        course=Course(name=data['name'],url=data['url'],descrip=data['desc'],ctype=data['type'])
        db.session.add(course)
        scourse=Course.query.filter(Course.name==data['name']).first()
        for i in data['tags']:
            sql="insert into course_tag(courseid, tagid) values(%d,%d)"%(scourse.id,int(i))
            db.session.execute(sql)
            for i in data['ctype']:
                sql1="insert into course_cate(courseid, course_typeid) values(%d,%d)"%(scourse.id,int(i))
                db.session.execute(sql1)
            db.session.commit()
    except:
        db.session.rollback()
    return jsonify({"code": 200, "msg": "添加成功"})


import math
@cc_blue.route("/getCourse")
def getCourse():
    page=int(request.args.get("page"))

    online=int(request.args.get("is_publish"))
    total=Course.query.filter(Course.is_publish==online).count()
    page_size=2
    start=math.ceil((page-1)*page_size)
    orderpara=request.args.get("orderpara")
    if orderpara == 'new':
        course=Course.query.order_by(Course.create_time.desc()).filter(Course.is_publish==online).offset(start).limit(page_size).all()
    else:
        course=Course.query.order_by(Course.views.desc()).filter(Course.is_publish==online).offset(start).limit(page_size).all()
    list=[{"id":i.id,"name":i.name,"url":i.url,"views":i.views} for i in course]
    return jsonify({"code":200,"clist":list,"total":total,"page_size":page_size})




@cc_blue.route("/getlevel")
def getLevel():
    ctype=int(request.args.get("ctype"))
    if ctype == 1:
        sql="select * from course where ctype=1"
        res=db.session.execute(sql)
        return jsonify({"code":200,"data":res})
    else:
        sql="select * from course where ctype=2"
        res=db.session.execute(sql)
        return jsonify({"code":200,"data":res})



